package com.hzh.blog.common.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hzh.blog.common.manager.CommonManager;
import com.hzh.blog.common.pager.Page;
import com.hzh.blog.common.pager.PageRequest;
import com.hzh.blog.common.pager.PropertyFilter;
import com.hzh.blog.common.web.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @Author:1984
 * @Written by:
 * @Creation date: 2011-3-1 下午03:26:19
 * @Version: V1.0
 * @Description: 所有action的父类,
 * 			主要负责检查权限
 */
public abstract class BaseAction extends ActionSupport{

	/**
	 * @Author:1984
	 * @Written by:
	 * @Creation date: 2011-2-22 下午03:19:07
	 * @Version: V1.0
	 * @Description:
	 */
	
	private static final long serialVersionUID = 1L;
	
	private String time;

	private String orderBy;

	private String orderDir;

	private int pageNo = 1;

	private int pageSize = 10;
	
	/**
	 * 不使用默认的分页查询逻辑的时候在这里指定MyBatis查询语句的
	 * 的statement ID 的后缀
	 */
	private String stateIDSuffix;

	public String getStateIDSuffix() {
		return stateIDSuffix;
	}

	public void setStateIDSuffix(String stateIDSuffix) {
		this.stateIDSuffix = stateIDSuffix;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	/**
	 * 获得默认的分页数据
	 */
	public void getDefaultPagerDataList(){
		
		Page page = getPage();

		List<PropertyFilter> filters = getFilterList();
		Page findPagerObjectListByFilter = getCommonManager().findPagerObjectListByFilter(filters, page);
		Struts2Utils.renderJson(findPagerObjectListByFilter);
	}
	
	public List<PropertyFilter> getFilterList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		return filters;
	}

	public Page getPage() {
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		if (orderBy != null && !orderBy.isEmpty())
			pageRequest.setOrderBy(orderBy);
		if (orderDir != null && !orderDir.isEmpty())
			pageRequest.setOrderDir(orderDir);
		
		Page page = new Page(pageRequest);
		return page;
	}
	
	/**
	 * 如果Action需要默认的分页方法需要实现此方法，初始化commonManager
	 * @param manager
	 */
	public abstract CommonManager getCommonManager();

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrderDir() {
		return orderDir;
	}

	public void setOrderDir(String orderDir) {
		this.orderDir = orderDir;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	
	
}